package cn.kgc.mybatis;

import cn.kgc.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author: 课工场
 * @since 2023/9/21
 * mybatis框架的基本使用
 *
 * 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的
 * SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得
 *
 */
public class MybatisDemo01 {

    public static void main(String[] args) {
        // 创建一个  SqlSessionFactory 对象   SqlSessionFactoryBuilder
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            // sqlSession
            SqlSession sqlSession = factory.openSession();
            List<User> users = sqlSession.selectList("a.selectAll");
            for (User user : users) {
                System.out.println("user = " + user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
